﻿using Microsoft.AspNet.SignalR.Hosting;
using Microsoft.IdentityModel.Tokens;
using SmartWMS.Common.Extensions;
using SmartWMS.Common.ResponseOutput;
using SmartWMS.Domain.BusinessManage;
using SmartWMS.Domain.FinanceManage;
using SmartWMS.Server.BusinessServer.AccountingServer.Dto;
using SmartWMS.Server.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartWMS.Server.BusinessServer.AccountingServer
{
    public class AccountingServers:IAccountingServers
    {
        private readonly IRepository<Accounting> _accounting;
        private readonly IRepository<PayType> _payType;

        public AccountingServers(IRepository<Accounting> accounting, IRepository<PayType> payType)
        {
            _accounting = accounting;
            _payType = payType;
        }


        /// <summary>
        /// 财务记账列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Response<List<AccountingDtoPas>>> GetAccountingDtoPas(AccountingDtoPage dto) {
            var plist = await _accounting.GetAllListAsync();
            var tlist = await _payType.GetAllListAsync();
            var list = (from a in plist
                        join b in tlist on a.ClassifyId equals b.Id
                        select new AccountingDtoPas
                        {
                            Id = a.Id,
                            SerialNo = a.SerialNo,
                            Name = a.Name,
                            ClassifyId = a.ClassifyId,
                            TypeName = b.TypeName,
                            PayOrCollection = a.PayOrCollection,
                            Type = a.Type,
                            PayPrice = a.PayPrice,
                            Paytime = a.Paytime,
                            PayInstitution = a.PayInstitution
                        }
                      ).ToList();

            if (!string.IsNullOrEmpty(dto.SerialNo)) { 
                list= list.Where(x => x.SerialNo == dto.SerialNo).ToList();
            }
            if (!string.IsNullOrEmpty(dto.Name))
            {
                list = list.Where(x => x.Name == dto.Name).ToList();
            }
            if (dto.ClassifyId > 0)
            {
                list = list.Where(x => x.ClassifyId == dto.ClassifyId).ToList();
            }
            if (!string.IsNullOrEmpty(dto.Paytimes))
            {
                list = list.Where(x => x.Paytime >= DateTime.Parse(dto.Paytimes)).ToList();
            }
            if (!string.IsNullOrEmpty(dto.Paytimee))
            {
                list = list.Where(x => x.Paytime <= DateTime.Parse(dto.Paytimee)).ToList();
            }
            int totalCount = list.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / dto.Size);
            list = list.Skip((dto.Page - 1) * dto.Size).Take(dto.Size).ToList();
            return new Response<List<AccountingDtoPas>> { Data = list, TotalCount = totalCount, PageCount = pageCount };

        }


    }
}
